package dxn.nl.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import dxn.boot.common.entity.CsBaseCodeNameDto;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.nl.file.biz.dao.AppModeOrgBindDao;
import dxn.nl.file.biz.dao.AppModeUserBindDao;
import dxn.nl.file.biz.dao.AppModelDao;
import dxn.nl.file.biz.po.AppModeOrgBindPo;
import dxn.nl.file.biz.po.AppModeUserBindPo;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.vo.CsApiModeVo;
import dxn.nl.file.biz.vo.CsExportModelUserBindVo;
import dxn.nl.file.client.model.enums.ExportFileTypeEnum;
import dxn.nl.file.service.CsExportAppModelService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CsExportAppModelServiceImpl implements CsExportAppModelService {

    @Resource
    private AppModelDao appModelDao;

    @Resource
    private AppModeOrgBindDao appModeOrgBindDao;

    @Resource
    private AppModeUserBindDao appModeUserBindDao;

    @Override
    public CsBasePageResult<AppModelPo> pageModeByModeVo(CsBasePageQuery<CsApiModeVo> pageQuery) {
        CsBasePageResult<AppModelPo> pageResult = new CsBasePageResult<>();
        List<AppModelPo> list = appModelDao.pageVo(pageQuery.getQuery());
        //pageResult.setTotal(list.size());
        pageResult.setRows(list);
        return pageResult;
    }

    @Override
    public AppModelPo selectByMode(String modeCode, int exportTypeCode) {
        LambdaQueryWrapper<AppModelPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModelPo::getModeCode, modeCode)
                .eq(AppModelPo::getTypeCode, exportTypeCode);
        return appModelDao.selectOne(lambdaQueryWrapper);
    }

    @Override
    public String addMode(AppModelPo po) {
        LambdaQueryWrapper<AppModelPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModelPo::getModeCode, po.getModeCode())
                .eq(AppModelPo::getTypeCode, po.getTypeCode());

        boolean exist = appModelDao.exists(lambdaQueryWrapper);
        if(exist)
        {
            return "模块编号已经存在";
        }

        appModelDao.insert(po);

        return null;
    }

    @Override
    public String modifyMode(AppModelPo po) {
        LambdaQueryWrapper<AppModelPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModelPo::getModeCode, po.getModeCode())
                .eq(AppModelPo::getTypeCode, po.getTypeCode());

        AppModelPo updatePo = new AppModelPo();
        updatePo.setId(po.getId());
        updatePo.setAppId(po.getAppId());
        updatePo.setModeName(po.getModeName());
        updatePo.setTag(po.getTag());
        updatePo.setRemark(po.getRemark());
        updatePo.setUpdateTime(LocalDateTime.now());
        updatePo.setUpdateUserCode(po.getUpdateUserCode());
        updatePo.setUpdateUserName(po.getUpdateUserName());
        updatePo.setTypeCode(po.getTypeCode());
        updatePo.setColumnList(po.getColumnList());

        appModelDao.updateById(po);
        return null;
    }

    @Override
    public String deleteMOde(AppModelPo po) {
        LambdaQueryWrapper<AppModeOrgBindPo> orgBindWrapper = new LambdaQueryWrapper<>();
        orgBindWrapper.eq(AppModeOrgBindPo::getModeCode, po.getModeCode())
                .eq(AppModeOrgBindPo::getExportTypeCode, po.getTypeCode());

        appModeOrgBindDao.delete(orgBindWrapper);

        LambdaQueryWrapper<AppModeUserBindPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModeUserBindPo::getModeCode, po.getModeCode())
                .eq(AppModeUserBindPo::getExportTypeCode, po.getTypeCode());

        appModeUserBindDao.delete(lambdaQueryWrapper);
        appModelDao.deleteById(po.getId());
        return null;
    }

    @Override
    public List<AppModelPo> selectAllByUserCode(String orgCode, String userCode, Integer exportTypeCode) {
        //return csExportAppModelDao.selectAllByUserCode(orgCode,userCode,exportTypeCode);
        return null;
    }

    @Override
    public boolean existUserBindMode(String userCode, String modeCode) {
        return true;
    }
}
